//
//  OAuthViewController.swift
//  01-SinaWeibo
//
//  Created by pc on 15/11/11.
//  Copyright © 2015年 file. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class OAuthViewController: UIViewController {


    
    //定义一个webView 属性
    let webView = UIWebView()
    
    //MARK 监听方法
    @objc private func close() {
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func fullAccount() {
        
        let jsString = "document.getElementById('userId').value = '18201593280' , document.getElementById('passwd').value = '1403557922';"
    
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    
    }
    
    
    
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    
        loadOauthPage()
    }

    //创建网络
    private func loadOauthPage(){
    
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
    
        let url = NSURL(string: urlString)
    
        let request = NSURLRequest(URL: url!)
    
        webView.loadRequest(request)
    }
    //授权页面
    private func setupUI(){
        
       self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "fullAccount")
        
    }
    

}

//专门处理 webView所有协议的方法
extension OAuthViewController: UIWebViewDelegate{

    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }

    //协议方法 如果返回 bool  一般表示 如果为ture 表示当前对象可以正常工作 否则就不能
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        print(request.URL)

        //打印了code 看一下
        
//        let urlString = request.URL?.absoluteString
//        
//        print(urlString)
        
        
        
        guard let urlString = request.URL?.absoluteString else{
            
            return false
        }

        
        //是网页请求页面 或者请求授权页面
        if urlString.hasPrefix("https://api.weibo.com") {
        
            return true
        }
        
        if !urlString.hasPrefix(redirect_uri) {
            
            return false
        }
        
        //程序走到这里 一定包含了 回调的url
        guard let qurey = request.URL?.query else {
        
            return false
        }
        
        let codeStr = "code="
        let code = qurey.substringFromIndex(codeStr.endIndex)
       
        print(qurey)
        
        print(code)
        
        UserAccountViewModel().loadAccessToken(code) { (error) -> () in
            
            print("come here")
            self.dismissViewControllerAnimated(true, completion: nil)
        }
        
        return false
    }

}




